package org.clockwise.driver;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.clockwise.feature.Account;
import org.clockwise.feature.AccountPool;
import org.clockwise.nlpir.ResultParser;
import org.clockwise.nlpir.TestNLPIR;
import org.clockwise.util.MyFileReader;
import org.clockwise.util.MyFileWriter;

public class AccountOperationDriver {
	
	
	
	/*
	 * This function reads the file and parse all legal lines.
	 */
	
	public static HashMap<Long, Account> getAccount() {
		String file = LinkerDriver.DATA_DIR + "Labeled_hot_users";
		HashMap<Long, Account> userdict = new HashMap<Long, Account>();
		MyFileReader mfr = new MyFileReader(file);
		String temp = null;
		while ((temp = mfr.getNextLine()) != null) {
			Account user = new Account(temp);
			userdict.put(user.id, user);
		}
		mfr.close();
		return userdict;
	}
	
	/*
	public static HashMap<Long, Account> getAccount() {
		String file = LinkerDriver.DATA_DIR + "hot_users_simplified";
		HashMap<Long, Account> userdict = new HashMap<Long, Account>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String temp = null;
			while ((temp = reader.readLine()) != null) {
				Account a = new Account(temp);
				userdict.put(a.id, a);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userdict;
	}
	*/
	public static AccountPool getAccountPool(HashMap<Long, Account> dict) {
		return new AccountPool(dict);
	}
	
	/*
	 * This function is deprecated, for it is just for test.
	 */
	public static void testAccount() {
		String file = LinkerDriver.DATA_DIR + "test";
		MyFileReader mfr = new MyFileReader(file);
		String output = LinkerDriver.DATA_DIR + "test1";
		MyFileWriter mfw = new MyFileWriter(output, true);
		String temp = null;
		int line = 0;
		while ((temp = mfr.getNextLine()) != null) {
			Account user = new Account(temp);
			mfw.write(user.toString() + "\n");
			line++;
		}
		System.out.println(line);
		mfr.close();
		mfw.close();
	}
	
	/*
	 * Warning: this function could rewrite a fatal data file.
	 * It should not be called frequently.
	 */
	public static void recordLabelAccount() {
		String file = LinkerDriver.DATA_DIR + "hot_users_simplified";
		ArrayList<Account> userlist = new ArrayList<Account>();
		ArrayList<String> userNameList = new ArrayList<String>();
		ArrayList<String> userInfoList = new ArrayList<String>();
		
		MyFileReader mfr = new MyFileReader(file);
		String temp = null;
		while ((temp = mfr.getNextLine()) != null) {
			Account a = new Account(temp);
			userlist.add(a);
			userNameList.add(a.name);
			userInfoList.add(a.info);
		}
		mfr.close();
		String[] userName = new String[userNameList.size()];
		String[] userInfo = new String[userInfoList.size()];
		userNameList.toArray(userName);
		userInfoList.toArray(userInfo);
		
		String[] nameResult = TestNLPIR.batchTest(userName);
		String[] infoResult = TestNLPIR.batchTest(userInfo);
		for (int i = 0; i < userlist.size(); i++) {
			Account user = userlist.get(i);
			String[][] name = ResultParser.parse(nameResult[i]);
			user.nameSegWord = name[0];
			user.nameSegLabel = name[1];
			String[][] info = ResultParser.parse(infoResult[i]);
			user.infoSegWord = info[0];
			user.infoSegLabel = info[1];
		}
		
		MyFileWriter mfw = new MyFileWriter(LinkerDriver.DATA_DIR + "Labeled_hot_users", true);
		
		Iterator<Account> iter = userlist.iterator();
		while (iter.hasNext()) {
			Account user = iter.next();
			if (user.name.contains("noname")) {
				continue;
			}
			mfw.write(user.toString() + "\n");
		}
		
		
		mfw.close();
		
		
	}
	
}
